/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cbsnclientparte;

import java.io.*;

/**
 *
 * @author Craig Butler (5668670)
 * @author Stephen Norman (616000X)
 * @version 1.0 21/05/2008
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // Leave a blank line for readability
        System.out.println();
        
        // Welcome the user to the application
        System.out.println("HIT2120 CBSN Client Part E");
        System.out.println("By Craig Butler (5668670) and Stephen Norman (616000X)");
        
        // Leave a blank line
        System.out.println();
        
        // Tell the user about GoBack-N
        System.out.println("This application shows an example GoBack-N protocol in action.");
        System.out.println("We will transmit approx. 1000 packets from the client to the server via a relay.");
        System.out.println("These packets may be corrupted, dropped or arrive out of order.");
        
        // Leave a blank line
        System.out.println();
        
        // Create a new protocol
        Protocol p = new Protocol();
        
        // Setup the protocol
        setupProtocol(args, p);
        
        // We're now ready to turn control over to the protocol's
        // automated process
        
        // From here on all code will be in the protocol class
        p.commenceAutomatedActions();
    }
    
    /**
     * Set's the relay's IP address in the provided protocol object
     * 
     * @param args The command line arguments of this application
     * @param p The protocol object to be setup
     */
    private static void setupProtocol(String[] args, Protocol p)
    {
        // Check for any command line arguments
        if (args.length == 2 && args[0].equals("-r"))
        {
            // If the relay's IP is provided in the command line
            // then set it here
            p.setRelayAddress(args[1]);
        }
        else   
        {
            try
            {
                // We need to find out what the IP address of the relay is

                // Used to hold the relay's address
                String relayAddress = "";

                // Create the ability to get input from the user
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                // Ask the relay's address
                System.out.print("Please enter the relay's IP address: ");
            
                // Get the response
                relayAddress = br.readLine();
                
                // Check the line contains something
                if (relayAddress.equals(""))
                    throw new Exception("No address was entered.");
                
                // No need to read anymore
                br.close();
                
                // Set the relay's address
                p.setRelayAddress(relayAddress);
            }
            catch (Exception ex )
            {
                // Display the error to the user
                System.out.println(Util.getTime() + "Input Exception: " + ex.getMessage());
                
                // We couldn't get the input
                // and so we cannot continue
                System.exit(1);
            }
        }
    }
}
